package com.blrs.Scheduling;

import com.blrs.domain.ActivityTraining;
import com.blrs.domain.QuestionnaireInvestigation;
import com.blrs.mapper.ActivityTrainingMapper;
import com.blrs.mapper.QuestionnaireInvestigationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {
    @Autowired
    private ActivityTrainingMapper activityTrainingMapper;
    @Autowired
    private QuestionnaireInvestigationMapper questionnaireInvestigationMapper;

    /**
     * 日期转换
     * @param date
     * @return
     */
    private LocalDateTime convertToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 活动培训定时任务
     */
    @Scheduled(cron = "0 */1 * * * ?") // 每小时执行一次
    public void updateCourseStatus() {
         List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingListNoExamine();
        LocalDateTime now = LocalDateTime.now();
        for (ActivityTraining activityTraining : activityTrainings) {
            LocalDateTime startTime = convertToLocalDateTime(activityTraining.getStartTime());
            LocalDateTime endTime = convertToLocalDateTime(activityTraining.getEndTime());
            LocalDateTime deadlineTime =activityTraining.getDeadlineTime();
            if (deadlineTime.isBefore(now) && activityTraining.getStatus().equals("报名")){
                activityTraining.setStatus("开课");
            }
            else if(startTime.isBefore(now) && endTime.isAfter(now)) {
                // 大于开始时间，小于结束时间，状态值为开课
                activityTraining.setStatus("开课");
            } else if (endTime.isBefore(now)) {
                // 大于结束时间，状态值为结束
                activityTraining.setStatus("结束");
            }
            // 更新数据库中的状态
            activityTrainingMapper.updateActivityTrainingStatus(activityTraining);
        }
        }

    /**
     * 问卷调查定时任务
     */
    @Scheduled(cron = "0 0 */1 * * ?") // 每小时执行一次
    public void updateQuestionnaire(){
     //查询当前问卷调查中不等于审核的
        List<QuestionnaireInvestigation> list=questionnaireInvestigationMapper.selectQuestionnaireInvestigationListExamine();
        LocalDateTime now = LocalDateTime.now();
        for (QuestionnaireInvestigation questionnaireInvestigation : list) {
            LocalDateTime localDateTime = convertToLocalDateTime(questionnaireInvestigation.getEndTime());
            if (localDateTime.isBefore(now)){
             questionnaireInvestigation.setStatus("结束");
            }
            questionnaireInvestigationMapper.updateQuestionnaireInvestigation(questionnaireInvestigation);
        }
    }

    /**
     * 培训活动定时任务,结果掉过了结束时间的活动
     */
    @Scheduled(cron = "0 */1 * * * ?") // 每小时执行一次
    public void updateActivityEnd(){
        activityTrainingMapper.updateActivityTrainingEnd();
    }



}
